package com.masterclient.app.businessmodule.usermodule.userinfo;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.middlelib.router.map.FCRouterPath;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.usermodule.userinfo.contract.UpdateUserContract;
import com.masterclient.app.businessmodule.usermodule.userinfo.contract.UpdateUserPresenter;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @auther hjian
 * @date 2020/8/12.
 */
@Route(path = FCRouterPath.USER_SIGNATURE)
public class UserSignatureActivity extends FCBusinessActivity<UpdateUserPresenter> implements UpdateUserContract.View{

    @BindView(R.id.user_signature_back)
    FrameLayout userSignatureBack;
    @BindView(R.id.user_signature_submit)
    TextView userSignatureSubmit;
    @BindView(R.id.user_signature_edit)
    EditText userSignatureEdit;
    @BindView(R.id.user_signature_num)
    TextView userSignatureNum;

    @Autowired(name = "content")
    String content;

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_user_signature;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setVisibility(View.GONE);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        userSignatureEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                userSignatureNum.setText(userSignatureEdit.getText().toString().length() + "/20");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        userSignatureEdit.setText(content);
    }

    @OnClick({R.id.user_signature_back, R.id.user_signature_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.user_signature_back:
                finish();
                break;
            case R.id.user_signature_submit:
                userSignatureSubmit.setEnabled(false);
                if (userSignatureEdit.getText().toString().trim().length()<2){
                    ToastUtil.showToast("个人签名至少2个字哦");
                    return;
                }
                Map<String, String> map = new HashMap<>();
                map.put("introduce", userSignatureEdit.getText().toString().trim());
                mPresenter.updateUserInfo(map);
                break;
        }
    }

    @Override
    public void updateUserResult(boolean isSuccess, String msg) {
        userSignatureSubmit.setEnabled(true);
        ToastUtil.showToast(msg);
        if (isSuccess){
            Intent intent = new Intent();
            intent.putExtra("introduce",userSignatureEdit.getText().toString().trim());
            setResult(RESULT_OK,intent);
            finish();
        }

    }
}
